<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:logger="http://www.matasek.net/DatabaseLogger"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="logger xsi">

    <xsl:output method="html" encoding="UTF-8" indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>

    <xsl:template match="/logger:table">
        <html>
            <head>
                <title>Historical view - <xsl:value-of select="/logger:table/@name"/>
                        (<xsl:value-of select="/logger:table/@dateTime"/>)</title>
                <!-- style inspired by PgAdmin application http://www.pgadmin.org/ -->
                <style type="text/css">
                    body {
                        font-family: verdana, helvetica, sans-serif;
                        margin: 0;
                        padding: 0;
                    }
                    h1 {
                        font-weight: bold;
                        font-size: 150%;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        margin-top: 0px;
                        padding-bottom: 0.5ex;
                        color: #eeeeee;
                    }
                    h2 {
                        font-size: 130%;
                        padding: 10px 10px 0.5ex;
                        margin-left: 10px;
                        margin-right: 10px;
                        color: #009ACE;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                    }
                    #header {
                        padding: 10px;
                        background-color: #009ACE;
                        color: #eeeeee;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        border-color: #999999;
                    }
                    #data {
                        margin-left: 10px;
                        margin-right: 10px;
                        margin-bottom: 10px;
                    }
                    #data td, th {
                        font-size: 80%;
                        margin-left: 2px;
                        margin-right: 2px;
                        padding: 2px 5px;
                    }
                    #data th {
                        border-bottom-color: #777777;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        background-color: #dddddd;
                        color: #009ACE;
                        vertical-align: top;
                        font-size: 80%;
                        white-space: nowrap;
                    }
                    .odd {
                        background-color: #dddddd;
                    }
                    .even {
                        background-color: #eeeeee;
                    }</style>
            </head>
            <body>
                <div id="header">
                    <h1>Historical view</h1>
                    <ul>
                        <li>
                            <strong>Table: </strong>
                            <xsl:value-of select="/logger:table/@name"/>
                        </li>
                        <li>
                            <strong>Date and time: </strong>
                            <xsl:value-of select="/logger:table/@dateTime"/>
                        </li>
                    </ul>
                </div>

                <h2>Data</h2>
                <xsl:choose>
                    <xsl:when test="count(logger:row) > 0">
                        <table id="data">
                            <tr>
                                <xsl:variable name="width">
                                    <xsl:value-of select="100 div count(logger:row[1]/logger:col)"/>
                                </xsl:variable>
                                <xsl:for-each select="logger:row[1]/logger:col">
                                    <th width="{$width}%">
                                        <xsl:value-of select="@name"/>
                                    </th>
                                </xsl:for-each>
                            </tr>
                            <xsl:for-each select="logger:row">
                                <tr>
                                    <xsl:attribute name="class">
                                        <xsl:choose>
                                            <xsl:when test="position() mod 2 = 0">even</xsl:when>
                                            <xsl:otherwise>odd</xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:attribute>
                                    <xsl:for-each select="*">
                                        <xsl:choose>
                                            <xsl:when test="@xsi:nil = 'true'">
                                                <td>
                                                  <em>NULL</em>
                                                </td>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <td>
                                                  <xsl:value-of select="."/>
                                                </td>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:for-each>
                                </tr>
                            </xsl:for-each>
                        </table>
                    </xsl:when>
                    <xsl:otherwise>
                        <p>no rows returned</p>
                    </xsl:otherwise>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
